﻿// -----------------------------------------------------------------------
// <copyright file="HighriseTests.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace DotNet37Signals.Client.Test.Bootstrappers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    using DotNet37Signals.Client.Highrise;
    using DotNet37Signals.Client.Highrise.Contracts;
    using DotNet37Signals.Client.Highrise.Models;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class HighriseTests
    {
        public void Run()
        {
            Console.WriteLine("----------------");
            Console.WriteLine(" Highrise Tests ");
            Console.WriteLine("----------------");

            this.Initialize();

            //this.Account();
            //this.Cases();
            //this.Categories();
            //this.Comments();
            this.Companies();
            //this.CustomFields();
            //this.Deals();
            //this.Emails();
            //this.Groups();
            //this.Memberships();
            //this.Notes();
            //this.Parties();
            //this.People();
            //this.Tags();
            //this.Tasks();
            //this.Users();

            Console.WriteLine("");
        }

        public HighriseClient Client { get; set; }

        public void Initialize()
        {
            var username = ConfigurationManager.AppSettings["username.highrise"];
            var password = ConfigurationManager.AppSettings["password"];
            var company = ConfigurationManager.AppSettings["company"];
            var keepAliveEnabled = Convert.ToBoolean(ConfigurationManager.AppSettings["keepAliveEnabled"]);

            var credentials = new Credentials(username, password, company, keepAliveEnabled);
            this.Client = new HighriseClient(credentials);
        }

        public void Account()
        {
            Console.WriteLine();
            Console.WriteLine("Account Contract Tests");
            Console.WriteLine("**********************");

            var service = this.Client.Account;

            var account = service.Get();

            Console.WriteLine("Account Name: {0}", account.Name);
        }

        public void Cases()
        {
            Console.WriteLine();
            Console.WriteLine("Cases Contract Tests");
            Console.WriteLine("********************");

            var service = this.Client.Cases;

            var cases = service.GetOpenList();

            Console.WriteLine("Cases Count: {0}", cases.Count);
        }

        public void Categories()
        {
            Console.WriteLine();
            Console.WriteLine("Categories Contract Tests");
            Console.WriteLine("*************************");

            var service = this.Client.Categories;

            var tasks = service.ListOfTasks();

            Console.WriteLine("Task Count: {0}", tasks.Count);

            var deals = service.ListOfTasks();

            Console.WriteLine("Deal Count: {0}", deals.Count);

            var task = service.GetTask(tasks.First().Id);

            Console.WriteLine("Task Name: {0}", task.Name);

            var deal = service.GetTask(deals.First().Id);

            Console.WriteLine("Deal Name: {0}", deal.Name);
        }

        public void Comments()
        {
            Console.WriteLine();
            Console.WriteLine("Comments Contract Tests");
            Console.WriteLine("***********************");

            var service = this.Client.Comments;
            var caseService = this.Client.Cases;
            var noteService = this.Client.Notes;

            var cases = caseService.GetOpenList();

            var notes = noteService.GetList(Subject.Cases, cases.First().Id);

            var comments = service.GetListByNotes(notes.First().Id);

            Console.WriteLine("Comments Count: {0}", comments.Count());
        }

        public void Companies()
        {
            Console.WriteLine();
            Console.WriteLine("Companies Contract Tests");
            Console.WriteLine("************************");

            var service = this.Client.Companies;

            var companies = service.GetList();

            Console.WriteLine("Companies Count: {0}", companies.Count());
        }

        public void CustomFields()
        {
            Console.WriteLine();
            Console.WriteLine("CustomFields Contract Tests");
            Console.WriteLine("***************************");

            var service = this.Client.CustomFields;

            var customFields = service.GetList();

            Console.WriteLine("CustomFields Count: {0}", customFields.Count());
        }

        public void Deals()
        {
            Console.WriteLine();
            Console.WriteLine("Deals Contract Tests");
            Console.WriteLine("********************");

            var service = this.Client.Deals;

            var deals = service.GetList();

            Console.WriteLine("Deals Count: {0}", deals.Count);
        }

        public void Emails()
        {
            Console.WriteLine();
            Console.WriteLine("Emails Contract Tests");
            Console.WriteLine("*********************");

            var service = this.Client.Emails;
            var caseService = this.Client.Cases;

            var cases = caseService.GetOpenList();

            var emails = service.GetList(Subject.Cases, cases.First().Id);

            Console.WriteLine("Emails Count: {0}", emails.Count());
        }

        public void Groups()
        {
            Console.WriteLine();
            Console.WriteLine("Groups Contract Tests");
            Console.WriteLine("*********************");

            var service = this.Client.Groups;
            
            var list = service.GetList();

            Console.WriteLine("Group Count: {0}", list.Count());

            var first = list.First();
            var group = service.Get(first.Id);

            Console.WriteLine("Group Name: {0}", group.Name);

            var id = service.Create("test");
            var newGroup = service.Get(id.Id);

            Console.WriteLine("New Group Name: {0}", newGroup.Name);

            service.Update(id.Id, "test2");
            var updateGroup = service.Get(id.Id);

            Console.WriteLine("Update Group Name: {0}", updateGroup.Name);

            service.Delete(id.Id);

            Console.WriteLine("Group Deleted");
        }

        public void Memberships()
        {
            Console.WriteLine();
            Console.WriteLine("Memberships Contract Tests");
            Console.WriteLine("**************************");

            var service = this.Client.Memberships;

            var memberships = service.GetList();

            Console.WriteLine("Memberships Count: {0}", memberships.Count());
        }

        public void Notes()
        {
            Console.WriteLine();
            Console.WriteLine("Notes Contract Tests");
            Console.WriteLine("********************");

            var service = this.Client.Notes;
            var caseService = this.Client.Cases;

            var cases = caseService.GetOpenList();

            var notes = service.GetList(Subject.Cases, cases.First().Id);

            Console.WriteLine("Notes Count: {0}", notes.Count());
        }

        public void Parties()
        {
            Console.WriteLine();
            Console.WriteLine("Parties Contract Tests");
            Console.WriteLine("**********************");

            var service = this.Client.Parties;

            var parties = service.Search();

            Console.WriteLine("Parties Count: {0}", parties.Count());
        }

        public void People()
        {
            Console.WriteLine();
            Console.WriteLine("People Contract Tests");
            Console.WriteLine("*********************");

            var service = this.Client.People;

            var person = service.Get(91013704);

            var firstName = "Steven";

            Console.WriteLine("Person Name: {0} {1}", person.FirstName, person.LastName);

            var people = service.SearchCriteria(p => p.FirstName == firstName && p.LastName == "Pardo");

            Console.WriteLine("Search Count: {0}", people.Count());
        }

        public void Tags()
        {
            Console.WriteLine();
            Console.WriteLine("Tags Contract Tests");
            Console.WriteLine("*******************");

            var service = this.Client.Tags;

            var tags = service.GetList();

            Console.WriteLine("Tags Count: {0}", tags.Count());
        }

        public void Tasks()
        {
            Console.WriteLine();
            Console.WriteLine("Tasks Contract Tests");
            Console.WriteLine("********************");

            var service = this.Client.Tasks;

            var tasks = service.GetListOfUpcoming();

            Console.WriteLine("Tasks Count: {0}", tasks.Count);
        }

        public void Users()
        {
            Console.WriteLine();
            Console.WriteLine("Users Contract Tests");
            Console.WriteLine("********************");

            var service = this.Client.Users;

            var me = service.Me();

            Console.WriteLine("My Name: {0}", me.Name);

            var list = service.GetList();

            Console.WriteLine("User Count: {0}", list.Count());

            var i = service.Get(me.Id.ToString());

            Console.WriteLine("My Name Again: {0}", i.Name);
        }

        private string GetLastName()
        {
            return "Pardo";
        }
    }
}
